package com.xmcowell.sportpacemaster.utils.action;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;

import com.xmcowell.sportpacemaster.layout.multi.entity.VideoInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 实现AbstructProvider接口，通过cursor来搜索视频的相关信息
 * Created by xmsz on 2016/12/1.
 */

public class VideoProvider implements AbstructProvider {
    private Context context;

    public VideoProvider(Context context) {
        this.context = context;
    }

    @Override
    public List<VideoInfo> getList() {
        List<VideoInfo> infos = null;
        if (context != null) {
            Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,
                    null, null);
            if (cursor != null) {
                infos = new ArrayList<>();
                while (cursor.moveToNext()) {
                    int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
                    String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
                    String album = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM));
                    String artist = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));
                    String displayName = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
                    String mimeType = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
                    String path = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                    long duration = cursor
                            .getInt(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
                    long size = cursor
                            .getLong(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
                    VideoInfo info = new VideoInfo();
                    info.setId(id);
                    info.setTitle(title);
                    info.setAlbum(album);
                    info.setArtist(artist);
                    info.setDisplayName(displayName);
                    info.setMimeType(mimeType);
                    info.setPath(path);
                    info.setDuration(duration);
                    info.setSize(size);
                    infos.add(info);
                }
                cursor.close();
                infos = filterVideo(infos);//过滤掉0kb的文件
            }
        }
        return infos;
    }

    /**
     * 小于10b的过滤掉
     * 过滤视频文件
     *
     * @param videoInfos
     * @return
     */
    private List<VideoInfo> filterVideo(List<VideoInfo> videoInfos) {
        List<VideoInfo> newVideos = new ArrayList<VideoInfo>();
        for (VideoInfo videoInfo : videoInfos) {
            File f = new File(videoInfo.getPath());
            if (f.exists() && f.isFile() && f.length() > 10) {
                newVideos.add(videoInfo);
                Log.i("TGA", "文件大小" + f.length());
            } else {
                Log.i("TGA", "文件太小或者不存在");
            }
        }
        return newVideos;
    }
}
